package com.zxl.yjb.activity.im;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.zxl.yjb.GlideApp;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.home.order.OrderDetailActivity;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.bean.MessageEvent;
import com.zxl.yjb.bean.OrderDetailBean;
import com.zxl.yjb.http.HostUrl;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.CheckApplacationUtil;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.PickerViewUtils;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.utils.ToastUtils;
import com.zxl.yjb.voiceplayer.EaseChatRowVoicePlayer;
import com.zxl.yjb.voiceplayer.IMAudioManager;
import com.zxl.yjb.widget.CircleImageView;
import com.zxl.yjb.widget.MapContainer;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DialogActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.iv_confirm2)
    ImageView iv_confirm2;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;
    @BindView(R.id.tv_orderdetail_id)
    TextView tv_orderdetail_id;
    @BindView(R.id.tv_order_type)
    TextView tv_order_type;
    @BindView(R.id.tv_order_endtime)
    TextView tv_order_endtime;
    @BindView(R.id.map_location)
    MapView map_location;
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.rv_pic)
    RecyclerView rv_pic;
    @BindView(R.id.tv_reedit)
    TextView tv_reedit;
    @BindView(R.id.tv_cancelorder)
    TextView tv_cancelorder;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.tv_daohang)
    TextView tv_daohang;
    @BindView(R.id.iv_head_img)
    CircleImageView iv_head_img;
    @BindView(R.id.ll_voice)
    LinearLayout ll_voice;
    @BindView(R.id.ll_amount)
    LinearLayout ll_amount;
    @BindView(R.id.tv_amount)
    TextView tv_amount;
    @BindView(R.id.scroll_view)
    NestedScrollView scroll_view;
    @BindView(R.id.map_container)
    MapContainer map_container;

    private AMap aMap;
    private BitmapDescriptor mBitmapDescriptor;

    private AnimationDrawable animationDrawable;

    private DataManager dataManager;
    private String order_sn;
    private LoginSucessBean userInfo;
    private OrderDetailBean orderDetail;
    private String charge_id;
    private String amount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this, R.layout.activity_order_detail, null);
        view.setBackgroundColor(getResources().getColor(R.color.translucent));
        view.setPadding(30,30,30,30);
        setContentView(view);
        ButterKnife.bind(this);
        IMAudioManager.instance().init(this);
        order_sn = getIntent().getStringExtra("id");
        charge_id = getIntent().getStringExtra("charge_id");
        amount = getIntent().getStringExtra("amount");
        userInfo = (LoginSucessBean) SaveBeanUtil.getBean(this, "vipInfo");
        animationDrawable = (AnimationDrawable) ll_voice.getBackground();
        ll_amount.setVisibility(View.VISIBLE);
        initView();
        initMap(savedInstanceState);
        getOrderDetail();
    }

    @Override
    protected void initView() {
        tv_page_title.setText("订单详情");
        iv_back.setVisibility(View.GONE);
        tv_reedit.setText("拒绝");
        tv_reedit.setVisibility(View.VISIBLE);
        tv_cancelorder.setText("接单");
        tv_daohang.setVisibility(View.GONE);

        map_container = (MapContainer) findViewById(R.id.map_container);
        map_container.setScrollView(scroll_view);
    }


    private void getOrderDetail() {
        if (TextUtils.isEmpty(order_sn))
            return;
        dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("order_sn", order_sn + "");
        ObserverOnNextListener<OrderDetailBean> listener = new ObserverOnNextListener<OrderDetailBean>() {
            @Override
            public void onNext(OrderDetailBean orderDetailBean) {
                setData(orderDetailBean);
            }
        };
        dataManager.orderDetail(new ProgressObserver<OrderDetailBean>(listener, this), map);
    }


    private void setData(OrderDetailBean orderDetail) {
        this.orderDetail = orderDetail;
        tv_orderdetail_id.setText(orderDetail.getOrder_sn());
        tv_order_type.setText(orderDetail.getCat_type_name()+"_"+orderDetail.getServer_name());
        tv_order_endtime.setText(orderDetail.getEnd_time()+"");
        tv_content.setText(orderDetail.getOrign_remark());
        GlideApp.with(this).load(orderDetail.getFace_img()).into(iv_head_img);
        tv_amount.setText(amount+"元");
        if (TextUtils.isEmpty(orderDetail.getVoice()))
        {
            ll_voice.setVisibility(View.GONE);
        }
        else
        {
            ll_voice.setVisibility(View.VISIBLE);
        }

        this.mBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.zy_dtdw);
        String latitude = orderDetail.getLatitude();
        String longitude = orderDetail.getLongitude();
        if (!TextUtils.isEmpty(latitude) && !TextUtils.isEmpty(longitude)) {
            LatLng latLng = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
            MarkerOptions markerOptions = (new MarkerOptions()).position(latLng).icon(this.mBitmapDescriptor);
            aMap.addMarker(markerOptions);
            CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 15, 0, 0));
            aMap.moveCamera(cameraUpdate);
        }
    }


    private void initMap(Bundle savedInstanceState) {
        map_location.onCreate(savedInstanceState);// 此方法必须重写
        aMap = map_location.getMap();
        aMap.getUiSettings().setMyLocationButtonEnabled(false);//设置默认定位按钮是否显示，非必需设置。
//        aMap.getUiSettings().setZoomControlsEnabled(false);
        aMap.getUiSettings().setAllGesturesEnabled(false);
        aMap.getUiSettings().setZoomGesturesEnabled(true);
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));

    }

    @OnClick({R.id.iv_back, R.id.tv_confirm,R.id.iv_confirm2,R.id.tv_reedit,R.id.tv_cancelorder})
    public void onViewClicked(View view) {
        Map<String, String> map = new HashMap<>();
        map.put("order_sn", order_sn);
        map.put("charge_id",charge_id);
        map.put("amount",amount);
        map.put("subject", "一键帮接单");
        switch (view.getId()) {
            case R.id.iv_back:
//                finish();
                break;
            case R.id.tv_confirm:
//                RongChatUtils.startPrivateChat(OrderDetailActivity.this, user_id, user_id, null);

                break;
            case R.id.iv_confirm2:

                break;
            case R.id.tv_reedit://拒绝
                map.put("accept1refuse2","2");
                accept(map);
                break;
            case R.id.tv_cancelorder://接单
                map.put("accept1refuse2","1");
                accept(map);
                break;

            case R.id.ll_voice:
                if (animationDrawable!=null&&!animationDrawable.isRunning())
                    animationDrawable.start();
                IMAudioManager.instance().playSound(HostUrl.HOSTPIC + orderDetail.getVoice(), new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        if (animationDrawable!=null&&animationDrawable.isRunning())
                            animationDrawable.stop();
                        ll_voice.setBackground(getResources().getDrawable(R.drawable.voice_anim));
                    }
                });
                /*EaseChatRowVoicePlayer voicePlayer = EaseChatRowVoicePlayer.getInstance(DialogActivity.this);
                voicePlayer.play(HostUrl.HOSTPIC + orderDetail.getVoice(), new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        if (animationDrawable!=null&&animationDrawable.isRunning())
                            animationDrawable.stop();
                        // Stop the voice play animation.
                    }
                });*/
                break;
        }
    }



    /**
     * 接单
     */
    private void accept(Map<String,String> map) {
        EventBus.getDefault().post(new MessageEvent("notshow"));
        if (TextUtils.isEmpty(order_sn)) {
            ToastUtils.show(getApplicationContext(),"获取订单信息失败！");
            finish();
            return;
        }
        DataManager dataManager = new DataManager();
        dataManager.accept(new ProgressObserver<Object>(new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
//                ToastUtils.show(getApplicationContext(),o.toString());
            }
        }, this){
            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        },map);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
        }
        return true;
    }

}
